//
//  SLCollectionDemoController.m
//  Objective-C
//
//  Created by 陆承东 on 2019/2/20.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLCollectionDemoController.h"

#import "SLCollectionViewFlowLayout.h"

@interface SLCollectionDemoController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation SLCollectionDemoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupView];
}

- (void)setupNavBar {
    self.navTitle = @"UICollectionViewDemo";
}

- (void)setupView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 30;
    flowLayout.minimumInteritemSpacing = 10;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.allowsMultipleSelection = NO;
    collectionView.backgroundColor = [UIColor greenColor];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.view addSubview:collectionView];
    [collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(kNavigationBarHeight);
    }];
}

#pragma mark - UICollectionView's delegate & datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[UICollectionViewCell alloc] init];
    }
    
    cell.contentView.backgroundColor = [UIColor blueColor];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 5 == 0) {
        return CGSizeMake(100, 100);
    } else if (indexPath.row % 5 == 1) {
        return CGSizeMake(80, 80);
    } else if (indexPath.row % 5 == 2) {
        return CGSizeMake(60, 60);
    } else if (indexPath.row % 5 == 3) {
        return CGSizeMake(40, 40);
    } else {
        return CGSizeMake(20, 20);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

// 设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
//// 列间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 10;
//}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"----------> SLCollectionDemoController - viewForSupplementaryElementOfKind: %@", kind);
    return nil;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor yellowColor];
    
    NSArray *indexPathsForSelectedItems = [collectionView indexPathsForSelectedItems];
    NSLog(@"----------> SLCollectionDemoController - didSelectItemAtIndexPath - indexPathsForSelectedItems: %@", indexPathsForSelectedItems);
    [collectionView deselectItemAtIndexPath:indexPath animated:true];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blueColor];
    
    NSArray *indexPathsForSelectedItems = [collectionView indexPathsForSelectedItems];
    NSLog(@"----------> SLCollectionDemoController - didDeselectItemAtIndexPath - indexPathsForSelectedItems: %@", indexPathsForSelectedItems);
}

@end
